package generatorJsonHtml;

import com.baomidou.mybatisplus.annotation.TableField;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;


public class testGenerator {

    public static void main(String[] args) throws IOException {

        ReadWriteLock rdLock = new ReentrantReadWriteLock();

        try {

            rdLock.readLock().lock();

            //相关信息填入
            Map<String, Object> infoMap = new HashMap<>();
            infoMap.put("tableName", "prj_project"); //表名
            infoMap.put("storeName", "my_java");  //库名
            Object need2GeneratorObj = new PrjProject(); //需要生成html对应的实体
            String span = "\"12\""; //样式间隔大小
            String px = "\"180\""; //大小（px）
            String bz = "\"未定义的标签\""; //默认中文标签名称


            /**
             * 获取表名库名等信息
             */
            String resource = "generator-html-datasource.xml";  //固定属性，不变更,如需变更数据库链接信息可以通过该文件进行修改

            Reader reader = Resources.getResourceAsReader(resource); //读取属性

            SqlSessionFactory sqlMapper = new
                    SqlSessionFactoryBuilder().build(reader); //SqlSession工厂生成

            SqlSession session = sqlMapper.openSession();  //开启Session

            List<Map<String, Object>> infoListMapRes =
                    session.selectList("generatorJsonHtml.TableInfoDao.getTableInfo", infoMap); //执行表信息查询


            /**
             * 获取表库名等信息后，进行处理生成element-ui
             */
            List<String> elementHtmlList = new ArrayList<>();

            Field[] fields = need2GeneratorObj.getClass().getDeclaredFields();

            for (Field field : fields) {

                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }

                if (!Objects.isNull(field.getAnnotation(TableField.class))) {

                    String itenValue = field.getAnnotation(TableField.class).value();
                    String valueName = field.getName();

                    //默认备注为自定义备注
                    String replaceBz = bz;

                    //查询是否有匹配的字段信息，如果有则赋值
                    String finalItenValue = itenValue;
                    Map<String, Object> entityColumn = infoListMapRes.stream()
                            .filter(v -> v.get("columnName").equals(finalItenValue)).findAny().orElse(null);

                    if (!Objects.isNull(entityColumn)
                            && !Objects.isNull(entityColumn.get("columnComment"))
                            && !"".equals(entityColumn.get("columnComment")))
                        replaceBz = (String) entityColumn.get("columnComment");

                    String a = "<el-row>  \n " +
                            "  <el-col :span=" + span + ">  \n " +
                            "    <el-form-item label-width=" + px + "label=" + "'" + replaceBz + "'" + ">  \n" +
                            "        <span>{{item." + valueName + "}}</span>  \n" +
                            "    </el-form-item>  \n" +
                            "  </el-col>  \n" +
                            "</el-row>";

                    elementHtmlList.add(a);
                }
            }

            System.out.println("-----------------------------------begin-------------------------------------");
            for (String s : elementHtmlList) {
                System.out.println(s + "\n");
            }
            System.out.println("------------------------------------end--------------------------------------");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rdLock.readLock().unlock();
        }

    }
}
